/*
 * 文件名：Dom4JUtil.java	 
 * 时     间：下午7:14:31
 * 作     者：Administrator       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.local.util;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.local.entity.vo.WeekdayVO;

/** 
 * 类描述:这里是类描述
 * @ClassName: Dom4JUtil 
 * @author Administrator
 * @date 2015年10月6日 下午7:14:31 
 */
public class Dom4JUtil
{
	
	private final static String BASE_PATH = "config/weekday.xml";  
    
    public String getConfFile(String file) {  
        URL confURL = getClass().getClassLoader().getResource(file);  
        if (confURL == null)  
            confURL = getClass().getClassLoader().getResource(  
                    "META-INF/" + file);  
        if (confURL == null)  
            confURL = Thread.currentThread().getContextClassLoader()  
                    .getResource(file);  
        if (confURL == null) {  
            System.err.println(" cann't find config file:-->" + file);  
        } else {  
            String filePath = confURL.getFile();  
            filePath = filePath.replaceAll("%20", " ");  
            File file1 = new File(filePath);  
            if (file1.isFile())  
                return filePath;  
        }  
        return null;  
    }  
      
    /** 
     * @title 获取工作日相关配置 
     * @author chanson 
     * @return 
     */  
    public WeekdayVO getWeekdayConfig(){  
          
        Map validateMap = new HashMap();  
        List weekendIsWeekdayList = new ArrayList();  
        List weekdayIsHolidayList = new ArrayList();  
          
        //1、放到web工程  
        File f = new File(getConfFile(BASE_PATH));  
        //2、application测试  
        //String file  = "D:/workspace/test/src/config/weekday.xml";  
        //File f = new File(file);  
        SAXReader reader = new SAXReader();  
        try{  
            Document doc = reader.read(f);  
            Element root = doc.getRootElement();  
              
            //===================================  
            //工作日校验相关属性  
            //===================================  
            Element validationElement = root.element("validation");  
            Element listElement;  
            for(Iterator i = validationElement.elementIterator("list");i.hasNext();){  
                listElement = (Element)i.next();  
                validateMap.put((String) listElement.elementText("key"),   
                        (String)listElement.elementText("value"));  
            }  
              
            //===================================  
            //工作日是假期的列表  
            //===================================  
            Element weekdayElement = root.element("weekday");  
            Element holidayListElement = weekdayElement.element("holiday_list");  
            Element holidayValueElement = null;  
            for(Iterator i = holidayListElement.elementIterator("date");i.hasNext();){  
                holidayValueElement = (Element)i.next();  
                weekdayIsHolidayList.add((String)holidayValueElement.getText());  
            }  
              
            //===================================  
            //周末是工作日的列表  
            //===================================  
            Element weekendElement = root.element("weekend");  
            Element weekdayListElement = weekendElement.element("weekday_list");  
            Element weekdayValueElement = null;  
            for(Iterator i = weekdayListElement.elementIterator("date");i.hasNext();){  
                weekdayValueElement = (Element)i.next();  
                weekendIsWeekdayList.add((String)weekdayValueElement.getText());  
            }  
              
            WeekdayVO vo = new WeekdayVO();  
            vo.setValidateMap(validateMap);  
            vo.setWeekdayIsHolidayList(weekdayIsHolidayList);  
            vo.setWeekendIsWeekdayList(weekendIsWeekdayList);  
            return vo;  
        }catch(Exception e){  
            e.printStackTrace();  
            return null;  
        }  
    }  
      //读取配置xml文件信息
    public static void main(String[] args) {  
        Dom4JUtil dom4JUtil = new Dom4JUtil();  
        WeekdayVO vo = dom4JUtil.getWeekdayConfig();  
        System.out.println(vo.getWeekdayIsHolidayList());  
        System.out.println(vo.getWeekendIsWeekdayList());  
    }  
	
}
